package com.reg.nstreg;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.json.JSONArray;
import org.json.JSONException;

public class WebControl {
    interface CallBack {
        void onFailure(String arg1, String arg2);

        void onResponse(int arg1, String arg2, String arg3);
    }

    public String _action;
    CallBack _callBack;
    public String _userAgent;
    Context c;
    public WebView webView;

    public WebControl(Activity arg1, String arg2, String arg3) {
        super();
        this._userAgent = arg2;
        this._action = arg3;
        this.c = ((Context)arg1);
    }

    public void setActiveUser(String arg3, final String arg4, final String arg5) {
        this.setConfigWeb();
        if(arg5 == null || (arg5.equals(""))) {
            this.webView.loadUrl(arg4);
        }
        else {
            CookieSyncManager.createInstance(this.webView.getContext());
            CookieManager v3 = CookieManager.getInstance();
            v3.setAcceptCookie(true);
            v3.removeAllCookie();
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                public void run() {
                    try {
                        StringBuilder v1 = new StringBuilder();
                        v1.append("");
                        v1.append(arg5);
                        JSONArray v0_1 = new JSONArray(v1.toString());
                        int v1_1;
                        for(v1_1 = 0; v1_1 < v0_1.length(); ++v1_1) {
                            CookieManager.getInstance().setCookie(v0_1.getJSONObject(v1_1).getString("url"), v0_1.getJSONObject(v1_1).getString("cookie"));
                        }
                    }
                    catch(JSONException v0) {
                        v0.printStackTrace();
                    }

                    CookieSyncManager.getInstance().sync();
                    WebControl.this.webView.loadUrl(arg4);
                }
            }, 500);
        }
    }

    void setConfigWeb() {
        this.webView = new WebView(this.c);
        this.webView.getSettings().setUserAgentString(this._userAgent);
        this.webView.clearCache(true);
        this.webView.getSettings().setJavaScriptEnabled(true);
        this.webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView arg1, int arg2) {
            }
        });
        this.webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView arg3, String arg4) {
                if(WebControl.this._callBack != null) {
                    WebControl.this._callBack.onResponse(200, WebControl.this._action, arg4);
                }
            }

            public void onReceivedError(WebView arg1, WebResourceRequest arg2, WebResourceError arg3) {
                super.onReceivedError(arg1, arg2, arg3);
                if(WebControl.this._callBack != null) {
                    WebControl.this._callBack.onFailure(arg3.toString(), WebControl.this._action);
                }
            }
        });
    }

    public void setOnCallBack(CallBack arg1) {
        this._callBack = arg1;
    }
}

